import { KeyBox} from "./KeyBox";
import { KeyListPane} from "./KeyListPane";

export class MainPasswordWindow {
    public keyListPane: KeyListPane;
    public keyBox: KeyBox = null;
    public passwdWnd: Isc.Window;
    private create: boolean;
    private checkPasswdForm: Isc.DynamicForm;
    constructor( keyListPane: KeyListPane) {
        this.keyListPane = keyListPane;
    }

    public showCreateMainPasswordWindow( keyBox: KeyBox = null) {
        this.keyBox = keyBox;
        const create = (keyBox == null);
        this.create = create;
        this.checkPasswdForm = isc.DynamicForm.create({
            fields: [{
                name: "password",
                required: true,
                title: "输入原主密码",
                type: "password",
                width: 200,
            }],
        });
        const form = isc.DynamicForm.create({
            fields: [
                {
                    name: "warning",
                    type: "blurb",
                    value: "注意：主密码一旦丢失，就全完了！一定要牢记主密码！",
                },
                {
                    hoverWidth: 200,
                    name: "password",
                    required: true,
                    title: "输入主密码：",
                    type: "password",
                    validators: [{
                        errorMessage: "两次输入密码不匹配",
                        otherField: "password2",
                        type: "matchesField",
                    },
                    {
                        condition: "value != null && value.length>=8",
                        errorMessage: "密码长度不能小于8",
                        type: "custom",
                    },
                ],
                    width: 200,
                },
                {
                    name: "password2",
                    required: true,
                    title: "确认主密码：",
                    type: "password",
                    width: 200,
                    wrapTitle: false,
                },
                {
                    align: "center",
                    click: this.createOrModifyMainKey,
                    name: "CreateButton",
                    title: create ? "创建主密码" : "更改主密码",
                    type: "button",
                },
            ],
            layoutAlign: "center",
            margin: 10,
        });
        if ( create) {
            this.checkPasswdForm.hide();
        }
        this.passwdWnd = isc.Window.create({
            height: 200,
            items: [ this.checkPasswdForm, form],
            showCloseButton: false,
            showHeaderIcon: false,
            showMinimizeButton: false,
            title: create ? "创建主密码" : "修改主密码",
            width: 500,
        });
        this.passwdWnd.centerInPage();
    }
    private createMainKey = (form2: Isc.DynamicForm, item: Isc.FormItem) => {
        isc.showPrompt( "正在创建主密码，请稍候...");
        setTimeout(() => {
            this.keyBox = KeyBox.create( form2.getItem( "password").getValue());
            this.keyListPane.showKeyTables( this.keyBox);
            isc.clearPrompt();
            this.passwdWnd.close();
        }, 100);
    }
    private createOrModifyMainKey = (form2: Isc.DynamicForm, item: Isc.FormItem): boolean => {
        if ( !form2.validate()) {
            return false;
        }
        if ( this.create) {
            this.createMainKey( form2, item);
        } else if ( this.checkPasswdForm.validate()) {
            if ( this.keyBox.checkPassword( this.checkPasswdForm.getItem( "password").getValue())) {
                this.keyBox.changeMainPassword( form2.getItem( "password").getValue());
                this.passwdWnd.close();
                isc.say( "修改主密码成功，请牢记新的主密码。");
            } else {
                this.checkPasswdForm.getItem( "password").setErrors( [ "原密码不正确，请重新输入！"]);
            }
        }
        return false;
    }
}
